/**
 * @(#) PacdpProxy.as
 *
 * @date		2012. 09. 19.
 * @version		1.0
 * @author		Park, Jutae
 * @description	 CDP신청 Proxy - Proxy
 * 				CDP 신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.pacdp.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.pacdp.PacdpFacade;
	import com.skt.common_hr.pacdp.model.vo.PacdpVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class PacdpProxy extends CommonProxy
	{
		public static const NAME:String = "PacdpProxy";
		
		public var pacdpList:ArrayCollection;
		public var sinChungYear:String;
		public var sinChungName:String;
		public var cbList:ArrayCollection;
		public var readList:ArrayCollection;
		public var sRettext:String;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function PacdpProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			
			loadData("Z_HR_CD01", params, loadResult, loadFault); 
		}
		
		public function readLoad(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			
			loadData("Z_HR_CD01", params, loadResult1, loadFault); 
		}
		
		
		public function insert(pernr:String, rec:PacdpVO):void
		{
			
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_CDPYR"] = sinChungYear; 	
			params["I_ACTIO"] = "INS"; 			//INS-신규, MOD-수정
			
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(rec);
			tables["ITAB"] = table;

			saveData("Z_HR_CD02", params, tables, insertResult, insertFault); 
		}

		public function update(pernr:String, rec:PacdpVO):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_CDPYR"] = rec.CDPYR;
			params["I_ACTIO"] = "MOD"; 		//INS-신규, MOD-수정	
			
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(rec);
			tables["ITAB"] = table;

			saveData("Z_HR_CD02", params, tables, updateResult, updateFault); 
		}

		public function del(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_CDPYR"] = sinChungYear; 			
			params["I_ACTIO"] = "DEL"; 	// D-삭제 
			
//			var tables:Object = new Object();
//			var table:Array = new Array();
//			table.push(rec);
//			tables["I_TABLE"] = table;

			loadData("Z_HR_CD02", params, delResult, delFault); 
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		
		public function loadResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				sinChungYear 	= event.result.O_CDPYR;
				pacdpList 		= parseCertiList(event.result.OTAB);
				cbList			= parseCb(event.result.T_CODE);
				sinChungName 	= event.result.O_ENAME;
				sRettext		= event.result.RETTEXT;
				facade.sendNotification(PacdpFacade.LOAD_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(PacdpFacade.LOAD_LIST_FAILED, this);
			}
		}
		
		public function loadResult1(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				readList 		= parseList(event.result.OTAB);
				facade.sendNotification(PacdpFacade.READ_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(PacdpFacade.READ_LIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(PacdpFacade.LOAD_LIST_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public var failedAlert:String; 
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(PacdpFacade.INSERT_COMPLETE, this);
			}
			else {
				failedAlert = event.result.RETTEXT;
				facade.sendNotification(PacdpFacade.INSERT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function insertFault(event:FaultEvent):void
		{
			facade.sendNotification(PacdpFacade.INSERT_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public var updateFailed:String; 
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(PacdpFacade.UPDATE_COMPLETE, this);
			}
			else {
				updateFailed = event.result.RETTEXT;
				facade.sendNotification(PacdpFacade.UPDATE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(PacdpFacade.UPDATE_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function delResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(PacdpFacade.DELETE_COMPLETE, this);
			}
			else {
				facade.sendNotification(PacdpFacade.DELETE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function delFault(event:FaultEvent):void
		{
			facade.sendNotification(PacdpFacade.DELETE_FAILED, this);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명 신청 현황 ArrayCollection
		 */
		private function parseCertiList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				
				var vo:PacdpVO = new PacdpVO();
				vo.CDPYR		= rec["CDPYR"];
				vo.IDATE		= DateUtil.stringToDateTime(rec["IDATE"]);
				vo.ATRACK 		= rec["ATRACK"];
				vo.ATRTXT		= rec["ATRTXT"];
				vo.APOTXT 		= rec["APOTXT"];
				vo.PERNR		= rec["PERNR"];
				vo.ENAME		= rec["ENAME"];
				vo.ASTAT	 	= rec["ASTAT"];
				vo.ASTATX		= rec["ASTATT"];
				vo.ABIGO		= rec["ABIGO"];
				
				// 변경삭제 유무 
				if(vo.ASTAT == '1' && vo.CDPYR == sinChungYear){
					vo.ASTAT_F = true;
				} else {
					vo.ASTAT_F = false;
				}	
				
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		private function parseList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				
				var vo:PacdpVO = new PacdpVO();
				vo.CDPYR		= rec["CDPYR"];
				vo.IDATE		= DateUtil.stringToDateTime(rec["IDATE"]);
				vo.ATRACK 		= rec["ATRACK"];
				vo.ATRTXT		= rec["ATRTXT"];
				vo.APOTXT 		= rec["APOTXT"];
				vo.PERNR		= rec["PERNR"];
				vo.ENAME		= rec["ENAME"];
				
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		private function parseCb(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			retArr.addItem({label:'Track 선택', data:-1});
			for each(var rec:Object in srcArr)
			{
				retArr.addItem({label:rec["TEXT"], data:rec["CODE"]});
			}
			return retArr;
		}
				
	}
}